/*
 * www.java
 *
 * Entry Point for net.kernub.www
 *
 */

package net.kernub.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.RootPanel;

import net.kernub.client.core.RPC;
import net.kernub.client.core.RPCLoadingHandler;

import net.kernub.client.UIGlobals;
import net.kernub.client.UserPageTitledManageLabel;
import net.kernub.client.UserPageTitledAddEditLink;
import net.kernub.client.UserPageTitledDisplayLinks;
import net.kernub.client.about.UserPageTitledAbout;

public class www implements EntryPoint
{
	public static int PAGE_DISPLAY_LINKS = 0;
	public static int PAGE_MANAGE_LABEL = 1;
	public static int PAGE_ADDEDIT_LINK = 2;
	public static int PAGE_EDITHOME = 3;
	public static int PAGE_ABOUT = 4;
	public static int PAGE_MAXNO = 5;

	private Page[] pages = new Page[PAGE_MAXNO];

	private static www gInstance = null;

	private int currentPage = PAGE_DISPLAY_LINKS;

	// onModuleLoad()
	public void onModuleLoad()
	{
		gInstance = this;

		RPC.setLoadingHandler( new RPCLoadingHandler() {
			public void onLoadingStart()
			{
				pages[currentPage].showLoading();
			}
			public void onLoadingStop()
			{
				pages[currentPage].hideLoading();
			}
		});

		UIGlobals.createInstance();

		pages[PAGE_DISPLAY_LINKS] = new UserPageTitledDisplayLinks();
		pages[PAGE_MANAGE_LABEL] = new UserPageTitledManageLabel();
		pages[PAGE_ADDEDIT_LINK] = new UserPageTitledAddEditLink();
		pages[PAGE_EDITHOME] = new UserPageTitledEditHome();
		pages[PAGE_ABOUT] = new UserPageTitledAbout();

		// we add default page by hand (no need to remove)
		RootPanel.get().add( pages[currentPage].getWidget() );
		pages[currentPage].setActive();
	}

	public static www instance()
	{
		return gInstance;
	}

	public void setPage( final int id )
	{
		if( id >= PAGE_MAXNO )
		{
			Window.alert( "Unable to set page ("+id+"): impossible, page id too big!" );
			return;
		}

		RootPanel.get().remove( pages[currentPage].getWidget() );
		RootPanel.get().add( pages[id].getWidget() );
		pages[id].setActive();
		this.currentPage = id;
	}

	/*
	public void setPage( Page page )
	{
		RootPanel.get().remove( this.currentPage.getWidget() );
		RootPanel.get().add( page.getWidget() );
		page.setActive();
		this.currentPage = page;
	}
	*/

	public Page getPage()
	{
		return pages[currentPage];
	}

	public String getPageName()
	{
		return pages[currentPage].getClass().getName();
	}
}


